package connect.service;

import io.netty.util.internal.ConcurrentSet;

public class Repeater {

	/**
	 * 此对象为消息转发器
	 * 用于中转消息 额 我在废话...
	 */
	
	
	private ConcurrentSet<Connection> list;//在线连接列表
	
	/**
	 * 构造
	 * @param list
	 */
	public Repeater(final ConcurrentSet<Connection> list) {
		this.list=list;
	}
	
	/**
	 *公屏聊天
	 * 此方法向所有在线连接发送消息
	 * @param msg
	 */
	public void sendContent(final Object msg) {
		for (Connection c : list) {
			c.getContext().writeAndFlush(msg);
		}
	}
	
	/**
	 * 私密聊天
	 * 此方法向指定连接发送消息
	 * @param key
	 * @param msg
	 */
	public void sendContent(final String key,final Object msg) {
		for (Connection c : list) {
			if (c.getKey().equals(key)) {
				c.getContext().writeAndFlush(msg);
			}
		}
	}
	
	/**
	 * 判断此连接是否在线
	 * @param key
	 * @return
	 */
	public boolean isOnline(final String key) {
		for (Connection c : list) {
			if (c.getKey().equals(key)) {
				return true;
			}
		}
		return false;
	}
	
	public ConcurrentSet<Connection> getList() {
		return list;
	}
}
